<?php

namespace app\admin\controller\xilujob;

use app\admin\model\xilujob\Area;
use app\common\controller\Backend;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 开放城市
 *
 * @icon fa fa-circle-o
 */
class SiteCity extends Backend
{
    protected $noNeedRight = ['select_city'];
    protected $searchFields = 'city_name';
    /**
     * SiteCity模型对象
     * @var \app\admin\model\xilujob\SiteCity
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\xilujob\SiteCity;
        $this->view->assign("statusList", $this->model->getStatusList());
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index()
    {
        return;
    }

    /**
     * 添加
     */
    public function add()
    {
        return;
    }

    public function select_city() {
        $name = $this->request->post('name','trim');
        $pagesize = $this->request->post('pageSize', 10);
        $page = $this->request->post('pageNumber', 1);

        //主键
        $primarykey = $this->request->request("keyField");
        //主键值
        $primaryvalue = $this->request->request("keyValue");
        if($primaryvalue){
            $query = Area::where('level','2')
                 ->where($primarykey,$primaryvalue);
        }else{
            $query = Area::where('level','2')
                ->where('id', 'not in', function($query){
                    $query->name('xilujob_site_city')->field('city_id');
                })
                ->field('id,shortname as name');
            if($name) {
                $query->where('name','like',"%$name%");
            }
        }
        $data = $query->paginate(['list_rows'=>$pagesize,'page'=>$page]);
        return ['list'=>$data->getCollection(), 'total'=>$data->total()];
    }

    public function edit($ids = null)
    {
        return;
    }
    public function del($ids = null)
    {
        return;
    }

    public function multi($ids = null)
    {
        return;
    }
}
